package com.qiguliuxing.dts.admin.controller;

import com.github.pagehelper.PageInfo;
import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.db.domain.DtsAddress;
import com.qiguliuxing.dts.service.base.DtsAddressService;
import com.qiguliuxing.dts.service.base.DtsRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理地址控制层
 * @author qiguliuxing
 */
@RestController
@RequestMapping("/admin/address")
@Validated
@Api(tags = ApiAdminConsts.Address.PARENT)
public class AdminAddressController {
    private static final Logger logger = LoggerFactory.getLogger(AdminAddressController.class);

    @Autowired
    private DtsAddressService addressService;

    @Autowired
    private DtsRegionService regionService;

    private Map<String, Object> toVo(DtsAddress address) {
        Map<String, Object> addressVo = new HashMap<>();
        addressVo.put("id", address.getId());
        addressVo.put("userId", address.getUserId());
        addressVo.put("name", address.getName());
        addressVo.put("mobile", address.getMobile());
        addressVo.put("isDefault", address.getIsDefault());
        addressVo.put("provinceId", address.getProvinceId());
        addressVo.put("cityId", address.getCityId());
        addressVo.put("areaId", address.getAreaId());
        addressVo.put("address", address.getAddress());
        String province = regionService.findById(address.getProvinceId()).getName();
        String city = regionService.findById(address.getCityId()).getName();
        String area = regionService.findById(address.getAreaId()).getName();
        addressVo.put("province", province);
        addressVo.put("city", city);
        addressVo.put("area", area);
        return addressVo;
    }

    @GetMapping("/list")
    @RequiresPermissions("admin:address:list")
    @RequiresPermissionsDesc(menu = {"用户管理", "收货地址"}, button = "查询")
    @ApiOperation(value = ApiAdminConsts.Address.LIST)
    @WebLog(description = ApiAdminConsts.Address.LIST, excludes = {LogParamsConsts.RESPONSE})
    public Object list(@ApiParam(value = "用户id") Integer userId,
                       @ApiParam(value = "用户名称") String name,
                       @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
                       @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
                       @ApiParam(value = "排序字段", defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @ApiParam(value = "排序规则", defaultValue = "desc") @Order @RequestParam(defaultValue = "desc") String order) {
        List<DtsAddress> addressList = addressService.querySelective(userId, name, page, limit, sort, order);
        long total = PageInfo.of(addressList).getTotal();

        List<Map<String, Object>> addressVoList = new ArrayList<>(addressList.size());
        for (DtsAddress address : addressList) {
            Map<String, Object> addressVo = toVo(address);
            addressVoList.add(addressVo);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", addressVoList);

        return ResponseUtils.ok(data);
    }
}
